<?php
namespace app\admin\model;
use think\db;
use think\Model;
class Rules extends Model
{

    /**
     *  获取所有列表
     * @return type
     */
    public function getAll()
    {
        return $this->order('sort', 'desc')->select()->toArray();
    }

    /**
     * 获取菜单
     */
    public function getById($id)
    {
        return (Object) $this->where('id', $id)->find()->toArray();
    }

    /**
     * 新增
     */
    public function add()
    {
        $opt        = new \stdClass;
        $opt->name  = input('post.name');
        $opt->sort  = input('post.sort');
        $opt->menus = implode(',', input('post.menus/a'));
        $result     = $this->save($opt);
        //日志
        $id         = $this->order("id desc")->find();
        $name = session('my_self')['name'];
        $des = $name."【添加】了编号为【".$id->id."】的角色";
        add_log(2,session('my_self')['id'], $des);
        if (false !== $result)
        {
            return js_return(1, "添加成功");
        }
        else
        {
            return js_return(0, $this->getError());
        }
    }

    /**
     * 编辑
     */
    public function edit()
    {
        $opt        = new \stdClass;
        $opt->name  = input('post.name');
        $opt->sort  = input('post.sort');
        $opt->menus = implode(',', input('post.menus/a'));

        $result = $this->update($opt, 'id=' . input('id'));
        //日志
        $id = input('post.id');
        $name = session('my_self')['name'];
        $des = $name."【修改】了编号为【".$id."】的角色";
        add_log(2,session('my_self')['id'], $des);
        if (false !== $result)
        {
            return js_return(1, "更新成功");
        }
        else
        {
            return js_return(0, $this->getError());
        }
    }

    public function del()
    {
        $result = $this->destroy(input('id'));
        if (false !== $result)
        {
            //日志
            $id = input('id');
            $name = session('my_self')['name'];
            $des = $name."【删除】了编号为【".$id."】的角色";
            add_log(2,session('my_self')['id'], $des);
            return js_return(1, "删除成功");
        }
        else
        {
            return js_return(0, $this->getError());
        }
    }

    
    public function rules_to_menus($rules_id)
    { 
        $rs=$this->where('id','in',$rules_id)->select()->toArray();
        $menus_child_id=[];
        foreach ($rs as $v)
        { 
            //获取子菜单id数组
            $menus_child_id= array_merge($menus_child_id,explode(',', $v['menus']));
        }
        
        $menus_child_id=array_unique($menus_child_id);
        $menus = new Menus();
        
        //获取所有菜单
        $rs = $menus->getAllMenus();
        $menus_main_id=[];//主菜单ID
        $menus_main=[];//主菜单
        $menus_child=[];//子菜单
        foreach ($rs as $v)
        {
            if(in_array($v['id'], $menus_child_id))
            { 
                //获取所有主菜单id
                $menus_main_id[]=$v['pid'];
            }
        }
        $menus_main_id=array_unique($menus_main_id);
        
        $menus_prefix = '/'. config('admin_name').'/';
        $allowUrls=[];
        foreach ($rs as $v)
        {
            if(in_array($v['id'], $menus_main_id))
            {
                $tmp = [];
                $tmp['id']=$v['id'];
                $tmp['name']=$v['name'];
                $tmp['alias']=$v['alias'];
                $tmp['child']=[];
                foreach ($rs as $c)
                {
                    if($c['pid']==$v['id'] && in_array($c['id'], $menus_child_id))
                    {
                        $tmp_ = [];
                        $tmp_['id']=$c['id'];
                        $tmp_['name']=$c['name'];
                        $tmp_['alias']= url($menus_prefix.$c['controller'].'/'.$c['action'].'/');
                        $tmp['child'][]=$tmp_;
                        $allowUrls[]= $c['controller'];
                    }
                }
                $menus_main[]=$tmp;
            }
            
        }     
        session('allowUrls', array_unique($allowUrls));
        return $menus_main;
    }


    public function rules_to_menus3($rules_id)
    {
        $m            = new Menus();
        $all_menus    = $m->getAllMenus();
        $rs           = $this->where('id', 'in', $rules_id)->select()->toArray();
        $my_menus_ids = [];
        foreach ($rs as $v) {
            $my_menus_ids = array_merge($my_menus_ids, explode(',', $v['menus']));
        }
        $my_menus_ids = array_unique($my_menus_ids);//我的菜单id
        $pids         = menu_parent_ids($my_menus_ids, $all_menus);//我的菜单父id

        $fli = array_filter($all_menus,function ($val)use($pids,$my_menus_ids){
            return in_array($val['id'],$my_menus_ids) || in_array($val['id'],$pids);
        });

        $my_menus_tree = menu2tree($fli, 'id', 'pid', 0);

        return $my_menus_tree;
    }

}
